package com.glut.controller;

import com.github.pagehelper.Page;
import com.glut.service.LevKldService;
import com.glut.service.SdfsQuestionsService;
import com.glut.vo.LevKld;
import com.glut.vo.SdfsQuestions;
import com.glut.vo.SecKld;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:沈林玉
 * @date:2020/2/9 15:48
 * @Description:
 */
@Controller
@RequestMapping("/Kld")
public class LevKldController {

    @Resource
    LevKldService levKldService;

    @Resource
    SdfsQuestionsService sdfsQuestionsService;

    /**
     * 1.查询所有一级知识点
     *
     * @param
     * @return
     */
    //@RequestMapping(value = "/selectDistrictList",method = {此方法可处理的请求类型},produces = {"响应内容的MIME类型})
    @RequestMapping(value = "/selectAllLev/{pageNum}", method = {RequestMethod.GET, RequestMethod.POST}, produces = {"application/json"})
    @ResponseBody   // 表示异步请求处理，不会跳转。自动将返回的结果使用fasterxml库转换json
    public List<LevKld> selectAllLevKld(LevKld levKld,
                                        @PathVariable Integer pageNum,
                                        HttpSession session) {
        if(pageNum==null||pageNum< 1){
            // 保留查询条件
            session.setAttribute("levKldCondition",levKld);
            pageNum=1;
        }else{
            // 点击分页
            levKld = (LevKld) session.getAttribute("levKldCondition");
        }
        List<LevKld> levKldList = (List<LevKld>)levKldService.selectAllLevKld(levKld,pageNum,100);

        return levKldList;
    }

    /**
     * 2.查询所有一级知识点(查询结果返回页面)
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/selectAllLevByRequest/{page}",method = {RequestMethod.GET,RequestMethod.POST})
    public String selectAllLevKldByRequest(LevKld levKld,
                                           ModelMap modelMap,
                                           @PathVariable("page") Integer pageNum,
                                           HttpSession session) {
        if(pageNum==null||pageNum< 1){
            // 保留查询条件
            session.setAttribute("levKldCondition",levKld);
            pageNum=1;
        }else{
            // 点击分页
            levKld = (LevKld) session.getAttribute("levKldCondition");
        }
        Page<LevKld> levKldPage = (Page<LevKld>) levKldService.selectAllLevKld(levKld,pageNum,10);

        modelMap.addAttribute("levKldList", levKldPage);

        return "kdl/levKdl_input";
    }

    /**
     * 3.查询所有二级知识点
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/selectAllSecByLevID/{pageNum}")
    @ResponseBody
    public List<SecKld> selectAllSecKld(SecKld secKld,
                                        ModelMap modelMap,
                                        @PathVariable Integer pageNum,
                                        HttpSession session) {
        if(pageNum==null||pageNum< 1){
            // 保留查询条件
            session.setAttribute("secKldCondition",secKld);
            pageNum=1;
        }else{
            // 点击分页
            secKld = (SecKld)session.getAttribute("secKldCondition");
        }
        Page<SecKld> secKldList = (Page<SecKld>)levKldService.selectAllSecKld(secKld,pageNum,10);

        return secKldList;
    }

    /**
     * 4.查询所有二级知识点(查询结果返回页面)
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/selectAllSecByLevIDRequest/{pageNum}")
    public String selectAllSecByLevIDRequest(SecKld secKld,
                                             ModelMap modelMap,
                                             @PathVariable Integer pageNum,
                                             HttpSession session) {
        if(pageNum==null||pageNum< 1){
            // 保留查询条件
            session.setAttribute("secKldCondition",secKld);
            pageNum=1;
        }else{
            // 点击分页
            secKld=(SecKld)session.getAttribute("secKldCondition");
        }
        Page<SecKld> secKldList = (Page<SecKld>)levKldService.selectAllSecKld(secKld,pageNum,10);

        // 二级知识点（已经级联一级知识点），存入map中返回
        modelMap.addAttribute("secKldList", secKldList);
        return "kdl/secKdl_input";
    }

    /**
     * 5.新增一级知识点
     *
     * @param levKld
     * @return
     */
    @RequestMapping("/insertLevKld")
    public String levKldInsert(LevKld levKld, HttpSession session) {
        int i = levKldService.levKldInsert(levKld);

        // 存入session中并进行查询
        session.setAttribute("insertLevKdl", i);

        return "redirect:/views/kdl/levKdl_input.jsp";
    }

    /**
     * 6.新增二级知识点
     *
     * @param secKld
     * @return
     */
    @RequestMapping("/insertSecKld")
    public String secKldInsert(SecKld secKld, HttpSession session) {
        int i = levKldService.secKldInsert(secKld);

        // 存入session中，页面再进行判断
        session.setAttribute("insertSecKdl", i);

        return "redirect:/views/kdl/secKdl_input.jsp";
    }


    /**
     * 7.更新一级知识点分类
     */
    @RequestMapping("/updateLevKld")
    @ResponseBody
    public List<Integer> levKldUpdate(LevKld levKld){
        int updateLev = levKldService.levKldUpdate(levKld);
        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(updateLev);
        return arrayList;
    }

    /**
     * 8.更新二级知识点分类
     */
    @RequestMapping("/updateSecKld")
    @ResponseBody
    public Map<String,Integer> secKldUpdate(SecKld secKld){
        int updateSec = levKldService.secKldDelete(secKld);
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("updateSec",updateSec);
        return map;
    }

    /**
     * 9.删除一级知识点分类(暂时不能删除，会违反多个表约束)
     */
    @RequestMapping("/deleteLevKld")
    @ResponseBody
    public Map<String,Integer>  levKldDelete(LevKld levKld){
        Map<String,Integer> map = new HashMap<String, Integer>();
        try {
            int delLevCtgr = levKldService.levKldDelete(levKld);
        } catch (Exception e) {
            map.put("delLevCtgr", 0);
            return map;
        }
        map.put("delLevCtgr",1);
        return map;


    }

    /**
     * 10.删除二级知识点分类
     */
    @RequestMapping("/deleteSecKld")
    @ResponseBody
    public Map<String,Integer> secKldDelete(SecKld secKld){

        // 查询此知识点是否已经被引用
        List<SdfsQuestions> sdfsQuestions = sdfsQuestionsService.selectAllSdfsQuestionsByKld(secKld.getSecKldID());

        Map<String,Integer> map = new HashMap<String, Integer>();
        try {
            // 若知识点未被引用，则删除
            if (sdfsQuestions.size()==0){
                int delSecCtgr = levKldService.secKldDelete(secKld);
                map.put("delSecCtgr",1);
                return map;
            }else {
                map.put("delSecCtgr", 0);
                return map;
            }
        } catch (Exception e) {
            map.put("delSecCtgr", 0);
            return map;
        }
    }

}
